package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spa.domain.entity.OrderDetail;
import com.spa.infrastructure.repository.condition.OrderDetailCondition;
import com.spa.infrastructure.repository.mapper.OrderDetailMapper;
import com.spa.infrastructure.repository.po.OrderDetailPO;
import org.springframework.stereotype.Component;
import java.util.List;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

/**
 * domain service
 * 订单详情表(OrderDetail) 
 *
 * @author lig
 * @since 2024-02-21 16:55:28
 */
@Component
public class OrderDetailRepositoryService extends MPJBaseServiceImpl<OrderDetailMapper, OrderDetailPO> {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public OrderDetailPO queryById(Long id) {
        return getById(id);
    }

    public OrderDetailPO queryByOrderId(Long id) {
        OrderDetailCondition condition = new OrderDetailCondition();
        condition.setOrderId(id);
        List<OrderDetailPO> poList = list(wrapper(condition));
        if(CollUtil.isNotEmpty(poList)){
            return poList.get(0);
        }
        return null;
    }

	/**
     * 分页查询
     * @return 查询结果
     */
	public Page<OrderDetailPO> pageListInfo(OrderDetailCondition condition) {
		LambdaQueryWrapper<OrderDetailPO> wrapper = wrapper(condition);
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper);
    }
	
	public List<OrderDetailPO> listInfo(OrderDetailCondition condition) {
        return list(wrapper(condition));
    }

	private LambdaQueryWrapper<OrderDetailPO> wrapper(OrderDetailCondition condition) {
        LambdaQueryWrapper<OrderDetailPO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getOrderId()), OrderDetailPO::getOrderId, condition.getOrderId());
        wrapper.in(CollectionUtil.isNotEmpty(condition.getOrderIds()), OrderDetailPO::getOrderId, condition.getOrderIds());
        return wrapper;
    }

    public void insert(OrderDetailPO orderDetailPO) {
        save(orderDetailPO);
    }

}

